import 'package:flutter_bloc/flutter_bloc.dart';

import 'GameEvent.dart';
import 'GameState.dart';

class GameBloc extends Bloc<GameEvent, GameStatesState> {
  GameBloc() : super(GameStatesState.initial()) {
    // 收集硬币
    on<CollectCoinEvent>((event, emit) {
      emit(state.copyWith(money: state.money + event.amount));
    });
    // 收集阳光
    on<CollectSunlightEvent>((event, emit) {
      emit(state.copyWith(sunlight: state.sunlight + event.sunlight));
    });
    // 花费阳光
    on<CostSunlightEvent>((event, emit) {
      emit(state.copyWith(
        sunlight: state.sunlight - state.selectedPlant!.costSunlight,
      ));
    });
    // 结束游戏
    on<GameOverEvent>((event, emit) {
      emit(state.copyWith(isGameOver: true));
    });
    // 更新选中的植物和按钮
    on<UpdateSelectedPlant>((event, emit) {
      emit(state.copyWith(selectedPlant: event.seedModel));
    });
    // 更新选中的植物和按钮
    on<ResetSelectedPlant>((event, emit) {
      emit(state.copyWith(selectedPlant: null));
    });
    // 增加僵尸轮次
    on<NextZombieRound>((event, emit) {
      emit(state.copyWith(zombieTimes: state.zombieTimes + 1));
    });
  }
}
